package 剑指offer.Month7_07.day14;

import java.util.ArrayList;

public class 从上往下打印二叉树 {
    ArrayList<ArrayList<Integer>> list = new ArrayList<>();
    int treeHeight = 0;
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> res = new ArrayList();
        if(root == null) return res;
        solve(root,0);
        for(int i = 0;i < list.size();i++){
            for(int j = 0;j < list.get(i).size();j++){
                res.add(list.get(i).get(j));
            }
        }
        return res;
    }

    public void solve(TreeNode root,int height){
        if(height >= treeHeight){
            list.add(new ArrayList());
            treeHeight++;
        }
        list.get(height).add(root.val);
        if(root.left != null) solve(root.left,height+1);
        if(root.right != null) solve(root.right,height + 1);
    }
}
